package com.zg.common.core.config;

import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 * 参数校验配置，支撑快速失败
 * @author Zxb
 */
@Configuration
public class ValidConfig {
    @Bean
    public Validator validator(AutowireCapableBeanFactory springFactory){
        ValidatorFactory factory = Validation.byProvider(HibernateValidator.class)
                .configure()
                //存在多个验证条件的情况下快速失败
                .failFast(true)
                .constraintValidatorFactory(new SpringConstraintValidatorFactory(springFactory))
                .buildValidatorFactory();
        return factory.getValidator();
    }
}
